package trab4.rpc;

import sim.core.* ;

import trab4.grid.rpc_msgs.*;
import trab4.transactions.*;
import trab4.transactions.rpc_msgs.*;

public class ClientTransactionStub implements TGridOperations {
	
	protected EndPoint client, server ;
	
	public ClientTransactionStub( EndPoint client, EndPoint server ) {		
		this.client = client ;
		this.server = server ;
	}
	
//-----CENTRALIZED TRANSACTIONS-------------------------------------------------------------//
	public long c_OpenTransaction() {
		TcpChannel ch = client.tcpSend( server, new Msg_C_OpenTransaction() ) ;
		if( ch == null ) throw new RemoteException("send failed. node is offline...") ;
		Msg_TransactionReturn res = ch.tcpRead() ;
		return res.value() ;
	}

	public void c_AbortTransaction( long tid) {
		TcpChannel ch = client.tcpSend( server, new Msg_C_AbortTransaction( tid ) ) ;
		if( ch == null ) throw new RemoteException("send failed. node is offline...") ;
		ch.tcpRead() ;
	}
	
	public TransactionResult c_CloseTransaction( long tid) {
		TcpChannel ch = client.tcpSend( server, new Msg_C_CloseTransaction( tid ) ) ;
		if( ch == null ) throw new RemoteException("send failed. node is offline...") ;
		Msg_TransactionReturn res = ch.tcpRead() ;
		return res.value()  ;
	}
		
//-----DISTRIBUTED TRANSACTIONS-------------------------------------------------------------//
	public long d_OpenTransaction() {
		TcpChannel ch = client.tcpSend( server, new Msg_D_OpenTransaction() ) ;
		if( ch == null ) throw new RemoteException("send failed. node is offline...") ;
		Msg_TransactionReturn res = ch.tcpRead() ;
		return res.value() ;
	}

	public void d_AbortTransaction( long tid) {
		TcpChannel ch = client.tcpSend( server, new Msg_D_AbortTransaction( tid ) ) ;
		if( ch == null ) throw new RemoteException("send failed. node is offline...") ;
		ch.tcpRead() ;
	}
	
	public TransactionResult d_CloseTransaction( long tid) {
		TcpChannel ch = client.tcpSend( server, new Msg_D_CloseTransaction( tid ) ) ;
		if( ch == null ) throw new RemoteException("send failed. node is offline...") ;
		Msg_TransactionReturn res = ch.tcpRead() ;
		return res.value()  ;
	}

//-----GRID OPERATIONS----------------------------------------------------------------------//
	public int readColor( long tid, int i, int j) {
		TcpChannel ch = client.tcpSend( server, new Msg_ReadColor( tid, i, j) ) ;
		if( ch == null ) throw new RemoteException("send failed. node is offline...") ;
		Msg_GridReturn res = ch.tcpRead() ;
		return res.value() ;
	}
	
	public int readShape( long tid, int i, int j) {
		TcpChannel ch = client.tcpSend( server, new Msg_ReadShape( tid, i, j) ) ;
		if( ch == null ) throw new RemoteException("send failed. node is offline...") ;
		Msg_GridReturn res = ch.tcpRead() ;
		return res.value() ;
	}
	
	public void writeColor( long tid, int i, int j, int v) {
		TcpChannel ch = client.tcpSend( server, new Msg_WriteColor( tid, i, j, v ) ) ;
		if( ch == null ) throw new RemoteException("send failed. node is offline...") ;
		ch.tcpRead() ;
	}
	
	public void writeShape( long tid, int i, int j, int v) {
		TcpChannel ch = client.tcpSend( server, new Msg_WriteShape( tid, i, j, v ) ) ;
		if( ch == null ) throw new RemoteException("send failed. node is offline...") ;
		ch.tcpRead() ;
	}

	public int[] gridSize( long tid ) {
		TcpChannel ch = client.tcpSend( server, new Msg_GridSize( tid ) ) ;
		if( ch == null ) throw new RemoteException("send failed. node is offline...") ;
		Msg_GridReturn res = ch.tcpRead() ;
		return res.value() ;
	}	
}
